<?php 
namespace Admin\Model;
use Think\Model;
class MusicModel extends Model{
	
	protected $_validate = array(
		array('musicName','require','音乐名称不能为空'), //默认情况下用正则进行验证
 
	);
	
	public function m_add(){
		if($this->create()){
			//查询是否 添加过
			$name = $_POST['musicName'];		
			$data = $this->where("musicName = '{$name}'")->find();
			
			if($data){
				$this->error = '添加失败，已有'.$data[0]['musicname'].'id为：'.$data[0]['id'];
			}else{
				$music = $this->upload('music','music','歌曲上传失败');	
				
				$data['musicName'] = $_POST['musicName'];
				$data['music'] = $music[0];
				$data['musicLyric'] = $_POST['musicLyric'];
				$data['musicAddTime'] = time();
				$data['authorId'] = $_POST['authorId'];
				$data['sortsId'] = $_POST['sortsId'];
				$data['shareId'] = $_POST['shareId'];
				$data['musicCount'] = $_POST['musicCount'];
				return $this->add($data);
			}
			
		}
	}
	
	//获取数据，可动态改变数据库
	public function get($table){
		$db = M($table);
		$data = $db->select();
		return $data;
	}
	
	
	//获取数据
	public function getAll($id=null,$table=null){
		//为空获取所以数据
		if(is_null($id)){
				$data = $this->select();
				return $data;			
		}
		//获取1条数据
		else{
			$data = $this->find($id);
			return $data; 
		}
	}
	
	//获取关联数据
	public function getJoin($id=null){
		if(is_null($id)){
			$data = $this->
			field('m_music.*,m_sorts.sortsname,m_author.authorname,m_author.authorImg,m_share.shareName')->
			join('left join m_sorts on m_music.sortsid = m_sorts.id')->
			join('left join m_author on m_music.authorId = m_author.id')->
			join('left join m_share on m_music.shareId = m_share.id')->
			select();
			return $data;
		}else{
			$data = $this->
			field('m_music.*,m_sorts.sortsname,m_author.authorname,m_author.authorImg,m_author.authorPic,m_share.shareName')->
			join('left join m_sorts on m_music.sortsid = m_sorts.id')->
			join('left join m_author on m_music.authorId = m_author.id')->
			join('left join m_share on m_music.shareId = m_share.id')->
			where('m_music.id ='.$id)->
			select();
			return $data;
		}
		
	}
	
	
	//修改数据
	public function edit(){
		if(I('post.authorId') == 0 || I('post.sortsId') == 0){
			$this->error = '歌手或者分类不能为空';
			return false;
		}
		if($this->create()){
			return $this->save();
		}
	}
	
	
	//删除数据
	public function del(){
		if($this->where('id='.I('get.id'))->delete()){
			return true;
		}
	}
	
	//音乐修改
	public function music(){
		if(empty($_FILES['music']['name'])){
			return $this->error = '音乐不能为空';
		}
		$music = $this->upload('music','music','音乐上传失败');
		//删除上一个音乐 
		unlink(I('music'));
		
		//组合post
		$_POST['music'] = $music[0];
		
		if($this->save($_POST)){
			return true;
		}
		
		
		
	}
	
	
	//封装上传类
	public function upload($name1=null,$name2=null,$txt=null){
		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize   =     0 ;// 设置附件上传大小
		$upload->exts      =     array('mp3', 'wma', 'flac', 'ape','ra','vqf','wv','m4a');// 设置附件上传类型
		$upload->rootPath  =     './Music/'; // 设置附件上传根目录
		// 上传文件 
		$info   =   $upload->upload(array($name1=>$_FILES[$name2]));
		if(!$info) {// 上传错误提示错误信息
			return	$this->error = $txt.$upload->getError();
		}else{// 上传成功
			foreach($info as $file){
				$data[]=$upload->rootPath.$file['savepath'].$file['savename'];
			}
			return $data;
		}
	}
}